package mkis.service.root.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import mkis.service.root.data.dao.TeamTouchDAO;
import mkis.service.root.data.model.BizMng;
import mkis.service.root.data.model.QuestionsDt;
import mkis.service.root.data.model.TeamTouch;
import mkis.service.root.data.model.TeamTouchDetail;
import mkis.service.root.data.model.TeamtouchCust;
import mkis.service.root.data.model.TeamtouchIdea;
import mkis.service.root.service.TeamTouchService;


@Service("teamTouchService")
public class TeamTouchServiceImpl implements TeamTouchService {

	@Resource(name="teamTouchDAO")
	private TeamTouchDAO teamTouchDAO;
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Override
	public List<TeamTouch> getTeamTouchList() throws Exception {
		return getTeamTouchList(null);
	}
	
	@Override
	public List<TeamTouch> getTeamTouchList(Map<String, Object> paramMap) throws Exception {
		return teamTouchDAO.getTeamTouchList(paramMap);
	}

	@Override
	public List<TeamTouch> getTeamTouchView(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getTeamTouchView(paramMap);
	}
	
	@Override
	public TeamTouchDetail getTeamTouchDetail(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getTeamTouchDetail(paramMap);
	}
	
	@Override
	public int getTeamTouchViewCount(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getTeamTouchViewCount(paramMap);
	}
	
	@Override
	public int getTeamTouchCount(Map<String, Object> paramMap) throws Exception {
		return teamTouchDAO.getTeamTouchCount(paramMap);
	}
	
	@Override
	public int multiSaveData(Map<String, Object> paramMap) throws Exception {
		logger.debug("paramMap : {} ",paramMap);
		
		Object modeObj=paramMap.get("mode");
		
		Map<String, Object> paramData=new HashMap<String, Object>();
		if(modeObj==null){
			modeObj="insert";
		}
		
		String mode=String.valueOf(modeObj);
		paramData.put("mode", paramMap.get("mode"));
		paramData.put("paramDay", paramMap.get("paramDay"));
		paramData.put("mb_id", paramMap.get("mb_id"));
		int result=0;
		if("insert".equals(mode)) {
			for(int a=1;a<=5;a++){
				String bizNoKey="bizNo"+a;
				String bizNameKey="bizName"+a;
				
				
				paramData.put("bizNo", paramMap.get(bizNoKey));
				paramData.put("bizName", paramMap.get(bizNameKey));
				
				if(a!=3 && a!=5){
					if(a!=4){
						for(int b=1;b<=4;b++) {
							String answerKey="answer"+a+b;//bizNo : 1,2
							paramData.put("answer", paramMap.get(answerKey));
							paramData.put("empSeq", b);
							//1,2번 insert
							teamTouchDAO.insertTeamtTouchEmpData(paramData);
						}
					} else{
						for(int b=1;b<=3;b++) {
							String answerKey="answer"+a+b;//bizNo : 4
							paramData.put("answer", paramMap.get(answerKey));
							
							//4번 insert
							teamTouchDAO.insertTeamtTouchIdeaData(paramData);
						}
					}
					teamTouchDAO.insertTeamtTouchData(paramData);
					result++;
					continue;
				} else if(a==3 && a!=5){
					for(int b=1;b<=3;b++) {
						String custNameKey="cust_name"+a+b;//bizNo : 3
						String birthdayDateKey="birthdayDate"+a+b;//bizNo : 3
						paramData.put("custName", paramMap.get(custNameKey));
						paramData.put("birthdayDate", paramMap.get(birthdayDateKey));
						//3번 항목 insert
						teamTouchDAO.insertTeamtTouchCustData(paramData);
						
					}
					teamTouchDAO.insertTeamtTouchData(paramData);
					result++;
					continue;
				} else {
					String personScoreKey="personScore"+a;
					paramData.put("personScore", paramMap.get(personScoreKey));
					
					//5번 항목 insert
					//insert
					teamTouchDAO.insertTeamtTouchData(paramData);
					result++;
					continue;
				}
				
			}
			
		} else {
			//delete 
			teamTouchDAO.deleteTeamtTouchEmpData(paramData);
			teamTouchDAO.deleteTeamtTouchIdeaData(paramData);
			teamTouchDAO.deleteTeamtTouchCustData(paramData);
			teamTouchDAO.deleteTeamtTouchData(paramData);
			for(int a=1;a<=5;a++){
				String bizNoKey="bizNo"+a;
				String bizNameKey="bizName"+a;
				//String answerKey="answer"+a;
				
				
				paramData.put("bizNo", paramMap.get(bizNoKey));
				paramData.put("bizName", paramMap.get(bizNameKey));
				
				if(a!=3 && a!=5){
					if(a!=4){
						for(int b=1;b<=4;b++) {
							String answerKey="answer"+a+b;//bizNo : 1,2
							paramData.put("answer", paramMap.get(answerKey));
							paramData.put("empSeq", b);
							//1,2번 insert
							teamTouchDAO.insertTeamtTouchEmpData(paramData);
						}
					} else{
						for(int b=1;b<=3;b++) {
							String answerKey="answer"+a+b;//bizNo : 4
							paramData.put("answer", paramMap.get(answerKey));
							
							//4번 insert
							teamTouchDAO.insertTeamtTouchIdeaData(paramData);
						}
					}
					teamTouchDAO.insertTeamtTouchData(paramData);
					result++;
					continue;
				} else if(a==3 && a!=5){
					for(int b=1;b<=3;b++) {
						String custNameKey="cust_name"+a+b;//bizNo : 3
						String birthdayDateKey="birthdayDate"+a+b;//bizNo : 3
						paramData.put("custName", paramMap.get(custNameKey));
						paramData.put("birthdayDate", paramMap.get(birthdayDateKey));
						//3번 항목 insert
						teamTouchDAO.insertTeamtTouchCustData(paramData);
						
					}
					teamTouchDAO.insertTeamtTouchData(paramData);
					result++;
					continue;
					
					
				} else {
					String personScoreKey="personScore"+a;
					paramData.put("personScore", paramMap.get(personScoreKey));
					
					//5번 항목 insert
					//insert
					teamTouchDAO.insertTeamtTouchData(paramData);
					result++;
					continue;
				}
				
			}
		}
		return result;
	}
	
	@Override
	public String getAuthType(String ss_mb_auth_code, String ss_mb_auth_level, String reg_auth_code, String reg_auth_level,String paramDay) {
		
		String authType="R";
		
		Date date=new Date(System.currentTimeMillis());
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		
		if("8".equals(ss_mb_auth_code)){
			authType="C";
			return authType;
		}
		
		String today=sf.format(date);
		
		if(!today.equals(paramDay)) {
			authType="R";
			return authType;
		}
		
		if("1".equals(reg_auth_code)) {
			//ss_mb_auth_code 팀장 C (2)
			if("2".equals(ss_mb_auth_code)) {
				authType="C";
			}
		} else if("2".equals(reg_auth_code)) {
			//ss_mb_auth_code 과장 C (3)
			if("3".equals(ss_mb_auth_code)) {
				authType="C";
			}
		} else if("3".equals(reg_auth_code)) {
			//ss_mb_auth_code 차장 C (4)
			if("4".equals(ss_mb_auth_code)) {
				authType="C";
			}
		} else if("4".equals(reg_auth_code) || "5".equals(reg_auth_code) || "6".equals(reg_auth_code)) {
			// ss_mb_auth_code 배원장/관리자 C  (7)
			if("7".equals(ss_mb_auth_code)) {
				authType="C";
			}
		} 

		return authType;
	}
	
	@Override
	public int multiSaveAnswerData(HttpServletRequest request) throws Exception {
		int result=0;
		Map<String,Object> paramMap=new HashMap<String, Object>();
		
		paramMap.put("paramDay", request.getParameter("paramDay"));
		paramMap.put("mb_id", request.getParameter("mb_id"));
		
		for(int a=1;a<=5;a++){
			String q_scoreKey="q_"+a;
			String q_score=request.getParameter(q_scoreKey);
			paramMap.put("q_score", q_score);
			paramMap.put("bizNo", a);
			result+=teamTouchDAO.updateTeamTouchScore(paramMap);
		}
		String mode=request.getParameter("mode");
		
		System.out.println("mode : "+mode);
		paramMap.put("remarks", request.getParameter("remarks"));
		
		if("update".equals(mode)) {
			teamTouchDAO.updateTeamTouchDetail(paramMap);
		} else {
			teamTouchDAO.insertTeamTouchDetail(paramMap);
		}
		
		return result;
	}
	
	@Override
	public int getAnswerChk(Map<String, Object> paramMap) throws Exception {
		return teamTouchDAO.getAnswerChk(paramMap);
	}
	@Override
	public int getTeamTouchViewDtCount(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getTeamTouchViewDtCount(paramMap);
	}
	
	@Override
	public List<TeamtouchCust> getContactCustList(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getContactCustList(paramMap);
	}
	
	@Override
	public List<QuestionsDt> getQuestionsList(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getQuestionsList(paramMap);
	}
	@Override
	public List<TeamtouchIdea> getIdeaList(Map<String, Object> paramMap)
			throws Exception {
		return teamTouchDAO.getIdeaList(paramMap);
	}
}
